package com.ikas.ai.server.module.appraise.service;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.appraise.dao.AppraiseSceneResultDAO;
import com.ikas.ai.server.module.appraise.dao.AppraiseSceneResultDetailDAO;
import com.ikas.ai.server.module.appraise.dto.req.SceneResultListReqDTO;
import com.ikas.ai.server.module.appraise.dto.resp.DashBoardOverViewSceneDetailRespDTO;
import com.ikas.ai.server.module.appraise.dto.resp.DashBoardOverViewSceneRespDTO;
import com.ikas.ai.server.module.appraise.dto.resp.SceneRespDTO;
import com.ikas.ai.server.module.appraise.dto.resp.SceneResultListRespDTO;
import com.ikas.ai.server.module.appraise.model.AppraiseSceneResult;
import com.ikas.ai.server.module.appraise.model.AppraiseSceneResultDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service("tAppraiseSceneResultService")
public class AppraiseSceneResultService extends ServiceImpl<AppraiseSceneResultDAO, AppraiseSceneResult> {

    @Autowired
    private AppraiseSceneResultDAO appraiseSceneResultDAO;
    @Resource
    private AppraiseSceneResultDetailService appraiseSceneResultDetailService;


    public List<SceneResultListRespDTO> list(SceneResultListReqDTO req) {
        LambdaQueryWrapper<AppraiseSceneResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CharSequenceUtil.isNotBlank(req.getSystemNo()), AppraiseSceneResult::getSystemNo, req.getSystemNo());
        wrapper.eq(null != req.getResultId() && req.getResultId() > 0, AppraiseSceneResult::getResultId, req.getResultId());
        wrapper.eq(null != req.getSceneId() && req.getSceneId() > 0, AppraiseSceneResult::getSceneId, req.getSceneId());
        List<AppraiseSceneResult> list = appraiseSceneResultDAO.selectList(wrapper);
        return list.stream().map(p -> {
            SceneResultListRespDTO resp = new SceneResultListRespDTO();
            BeanUtils.copyProperties(p, resp);
            return resp;
        }).collect(Collectors.toList());
    }

    public List<SceneRespDTO> findSceneByResultId(Long resultId) {
        LambdaQueryWrapper<AppraiseSceneResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != resultId && resultId > 0, AppraiseSceneResult::getResultId, resultId);
        List<AppraiseSceneResult> results = appraiseSceneResultDAO.selectList(wrapper);
        List<SceneRespDTO> result = com.ikas.ai.utils.BeanUtils.convert(results, SceneRespDTO.class).stream().distinct().collect(Collectors.toList());
        return result;
    }

    public List<DashBoardOverViewSceneRespDTO> overViewDetail(Long resultId) {
        LambdaQueryWrapper<AppraiseSceneResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppraiseSceneResult::getResultId, resultId);
        List<AppraiseSceneResult> list = appraiseSceneResultDAO.selectList(wrapper);
        return list.stream().map(p -> {
            DashBoardOverViewSceneRespDTO resp = new DashBoardOverViewSceneRespDTO();
            BeanUtils.copyProperties(p, resp);
            List<AppraiseSceneResultDetail> detailList = appraiseSceneResultDetailService.listBySceneResultId(p.getId());
            resp.setList(detailList.stream().map(p1 -> {
                DashBoardOverViewSceneDetailRespDTO detailRespDTO = new DashBoardOverViewSceneDetailRespDTO();
                BeanUtils.copyProperties(p1, detailRespDTO);
                detailRespDTO.setResultId(resultId);
                return detailRespDTO;
            }).collect(Collectors.toList()));
            return resp;
        }).collect(Collectors.toList());
    }

}